
** Loading data - generated in dofile: data05
use "$data\minwage_data5_estimation_2012_2015.dta", clear
keep if inrange(tline,-24,24) & apprentice == 0 // 49 event-month oberservation window & excluding apprentices

** Reducing the number of variables to save time
keep pnr tline emplyd felt_200_trim timeantF12_trim calc_hrly_wage cohort brthmnth tline month

replace emplyd = emplyd * 100
replace felt_200_trim = 0 if felt_200_trim == . // if no reported earnings, earnings set to zero
replace timeantF12_trim = 0 if timeantF12_trim ==. // if no reported hours, hours set to zero
gen emonth = ym(cohort, brthmnth) 

** Merging with data on parental income information generated in dofile data7
merge m:1 pnr using "$data\parental_income_ptiles10.dta", nogen keep(match master)  keepus(avg_p_earn_rank avg_parental_earnings)

 qui: tab tline, gen(Dtime) // 49 event months
 qui: tab month, gen(Dmnth) // 48 Calendar months
 qui: tab cohort, gen(Dcoh) // 8 birth cohorts
 qui: tab emonth, gen(Dbmonth) // 96 birth months
 ge tline_pos = tline+ 25

 egen cohmxmonth = group(tline month) // month cohorts 2352 
  
** Time dummies   
ge Dtime0 = tline>=0
ge DtimeZ = tline == 0

** Age polynomial
ge tline_pos_sq=tline_pos*tline_pos
ge tline_pos_cu=tline_pos*tline_pos*tline_pos
ge tline_pos_4th=tline_pos*tline_pos*tline_pos*tline_pos
ge tline_pos_5th=tline_pos*tline_pos*tline_pos*tline_pos*tline_pos

** counters I use to drop results in Excel document below.
local x = 3
local y = 4
local j = 35
local g = 39

** Baseline regression below used to generate data points in Figure 5 and Figure A.9
forval p = 1/10{	
	foreach var in emplyd calc_hrly_wage{
		if `var' == emplyd{
			local l e
		}
		if `var' == calc_hrly_wage{
			local l w
		}
	
		matrix define `l'p`p' = .
		matrix list `l'p`p'
	
		** Baseline regression 5 degree age polynomial including dummy at event time 0
		reg `var' tline_pos tline_pos_sq tline_pos_cu tline_pos_4th tline_pos_5th Dtime0 DtimeZ if avg_p_earn_rank == `p', vce(cl cohmxmonth)
		putexcel set "$out\Figure5B_dofile_reg04.xls", sheet(Earnings) modify
			#d; 
			putexcel E2=("Obs."); putexcel E`y' =(e(N));
			matrix b=[_b[Dtime0], _b[Dtime0] - 1.96*_se[Dtime0], _b[Dtime0] + 1.96*_se[Dtime0]];
			putexcel A`y'=("`var'"); putexcel B`y'=matrix(b);
			putexcel A`x' =("Parental Earnings: decile `p' "); putexcel B2=("Beta"); putexcel C2=("[95% Conf. Interval]"); 
			putexcel A34 =("Earnings sample"); putexcel A38 =("Full sample"); 
			global b _b[tline_pos]; global b1 _b[tline_pos_sq];
			global b2 _b[tline_pos_cu]; global b3 _b[tline_pos_4th]; 
			global b4 _b[tline_pos_5th]; 
			global c _b[_cons]; global D18 _b[Dtime0];
			#d cr
			#d cr
			
			** Prediction wages and employment at event time 0 with youth/adult minimum wage 
			matrix pred_17_`l'`p' = $c + $b * (25)  + $b1 * (25^2)  + $b2 * (25^3)  + $b3 * (25^4) + $b4 * (25^5) // youth min wage
			matrix pred_18_`l'`p' = $c + $b * (25)  + $b1 * (25^2)  + $b2 * (25^3)  + $b3 * (25^4) + $b4 * (25^5) + $D18 // adult min wage
			
		nlcom delta: _b[Dtime0] / (_b[_cons]+_b[tline_pos]*25+_b[tline_pos_sq]*25^2+_b[tline_pos_cu]*25^3+_b[tline_pos_4th]*25^4+_b[tline_pos_5th]*25^5+_b[Dtime0]*0.5), post
		matrix `l'p`p' = [_b[delta]]
		#d; 
			matrix b=[_b[delta], _b[delta] - 1.96*_se[delta], _b[delta] + 1.96*_se[delta]];
			putexcel I`y'=matrix(b);
			putexcel I2=("Delta"); putexcel J2=("[95% Conf. Interval]"); 
			#d cr
			
		sum avg_parental_earnings if avg_p_earn_rank == `p'
		putexcel F`y'= (r(mean))
		putexcel F2=("Average Earnings")
		
		sum `var' if avg_p_earn_rank == `p' & tline == -1
		putexcel G`y'= (r(mean))
		putexcel G2=("Average 17.11")
		
		if `p' == 10{
			** Parental Earnings sample (to check consistancy with full sample below: same results)
			reg `var' tline_pos tline_pos_sq tline_pos_cu tline_pos_4th tline_pos_5th Dtime0 DtimeZ if avg_p_earn_rank != ., vce(cl cohmxmonth)
			#d;
			matrix b=[_b[Dtime0], _b[Dtime0] - 1.96*_se[Dtime0], _b[Dtime0] + 1.96*_se[Dtime0]];
			putexcel A`j'=("`var'"); putexcel B`j'=matrix(b); putexcel E`j' =(e(N));
			#d cr
			
			nlcom delta: _b[Dtime0] / (_b[_cons]+_b[tline_pos]*25+_b[tline_pos_sq]*25^2+_b[tline_pos_cu]*25^3+_b[tline_pos_4th]*25^4+_b[tline_pos_5th]*25^5+_b[Dtime0]*0.5), post
			#d; 
			matrix b=[_b[delta], _b[delta] - 1.96*_se[delta], _b[delta] + 1.96*_se[delta]];
			putexcel I`j'=matrix(b);
			#d cr
			local j = `j' + 1
			
			** Full sample (to check consistancy with sligthly reduced sample above (missing parental characteristics fx migration/death): same results)
			reg `var' tline_pos tline_pos_sq tline_pos_cu tline_pos_4th tline_pos_5th Dtime0 DtimeZ, vce(cl cohmxmonth)
			#d;
			matrix b=[_b[Dtime0], _b[Dtime0] - 1.96*_se[Dtime0], _b[Dtime0] + 1.96*_se[Dtime0]];
			putexcel A`g'=("`var'"); putexcel B`g'=matrix(b); putexcel E`g' =(e(N));
			#d cr
			
			nlcom delta: _b[Dtime0] / (_b[_cons]+_b[tline_pos]*25+_b[tline_pos_sq]*25^2+_b[tline_pos_cu]*25^3+_b[tline_pos_4th]*25^4+_b[tline_pos_5th]*25^5+_b[Dtime0]*0.5), post
			#d; 
			matrix b=[_b[delta], _b[delta] - 1.96*_se[delta], _b[delta] + 1.96*_se[delta]];
			putexcel I`g'=matrix(b);
			#d cr
			local g = `g' + 1	
			
		local x = `x' + 1

		}
		
	local y = `y' + 1

	}
	local y = `y' + 1
	local x = `x' + 3
	matrix E`p' = (ep`p'/((wp`p'[1,1])*-1))
	matrix w`p' =  wp`p'[1,1]
	matrix em`p' =  ep`p'
  	
}

** Collecting results in matrix
matrix prediction = (1 , pred_17_e1, pred_18_e1, pred_17_w1, pred_18_w1 \ ///
					 2 , pred_17_e2, pred_18_e2, pred_17_w2, pred_18_w2 \ ///
					 3 , pred_17_e3, pred_18_e3, pred_17_w3, pred_18_w3 \ ///
					 4 , pred_17_e4, pred_18_e4, pred_17_w4, pred_18_w4 \ ///
					 5 , pred_17_e5, pred_18_e5, pred_17_w5, pred_18_w5 \ ///
					 6 , pred_17_e6, pred_18_e6, pred_17_w6, pred_18_w6 \ ///
					 7 , pred_17_e7, pred_18_e7, pred_17_w7, pred_18_w7 \ ///
					 8 , pred_17_e8, pred_18_e8, pred_17_w8, pred_18_w8 \ ///
					 9 , pred_17_e9, pred_18_e9, pred_17_w9, pred_18_w9 \ ///
					 10 , pred_17_e10, pred_18_e10, pred_17_w10, pred_18_w10)
svmat prediction
rename prediction1 rank
rename prediction2 emply1712
rename prediction3 emply1800
rename prediction4 wage1712
rename prediction5 wage1800


** Results on wages
matrix w = (1 , w1 \ 2, w2 \ 3, w3 \ 4, w4 \ 5, w5 \ 6, w6 \ 7, w7 \ 8, w8 \ 9, w9 \ 10, w10 ) 
svmat w
rename w2 delta_w
replace delta_w = delta_w*100
** Results on employment
matrix em = (1 , em1 \ 2, em2 \ 3, em3 \ 4, em4 \ 5, em5 \ 6, em6 \ 7, em7 \ 8, em8 \ 9, em9 \ 10, em10 ) 
svmat em
rename em2 delta_empl
replace delta_empl = delta_empl * 100
replace delta_empl = delta_empl*(-1)
** Elasticity results 
matrix E = (1 , E1 \ 2, E2 \ 3, E3 \ 4, E4 \ 5, E5 \ 6, E6 \ 7, E7 \ 8, E8 \ 9, E9 \ 10, E10 ) 
svmat E
rename E1 pearn
rename E2 e

drop if rank == . // dropping observations not needed for Figures below.

** Figure 5: Employment Elasticity by Worker Characteristics
** (b) Parental Income
two scatter e rank, color(black) msize(large) graphregion(color(white) margin(small)) ///
yti("Elasticity") ylabel(0.0(0.2)1.4, nogrid) yline(0.82, lc(color) sty(thin)) ///
xti("Parental income decile") xlabel(1(1)10) xtick(1(1)10) scale(0.85)   
graph export "$out\Figure5B_parental_income_elasticity_dofile_reg04.png", width(800) height(600) replace

** Figure A.9:Differences in Age and Employment Across Groups of Workers at Age 18
** (c) Wages by Parental Income Decile
two scatter wage1712 rank, color(black) msize(large) graphregion(color(white) margin(small)) || ///
	scatter wage1800 rank, color(black) msymbol(t) msize(large) ///
yti("Hourly wage (DKK)") ylabel(0(25)175, nogrid) ////
xti("Parental income decile") xlabel(1(1)10) xtick(1(1)10) ///
		legend(label(1 "Low minimum wage") label(2 "High minimum wage") cols(2) region(lcolor(white))) ///
	    scale(0.85)   
graph export "$out\Apndx_Figure9C_parental_income_wages_dofile_reg04.png", width(800) height(600) replace
** (d) Employment by Parental Income Decile
two scatter emply1712 rank, color(black) msize(large) graphregion(color(white) margin(small)) || ///
	scatter emply1800 rank, color(black) msymbol(t) msize(large) ///
yti("Predicted employment rate (%)") ylabel(20(10)80, nogrid) ////
xti("Parental income decile") xlabel(1(1)10) xtick(1(1)10) ///
		legend(label(1 "Low minimum wage") label(2 "High minimum wage") cols(2) region(lcolor(white))) ///
	    scale(0.85)   
graph export "$out\Apndx_Figure9D_parental_income_employment_dofile_reg04.png", width(800) height(600) replace
